package cn.ibobo.lease.app.controller;

import cn.ibobo.lease.app.service.impl.SmsCodeServiceImpl;
import cn.ibobo.lease.app.service.impl.UserServiceImpl;
import cn.ibobo.lease.common.dto.UserDTO;
import cn.ibobo.lease.common.enums.ResultEnum;
import cn.ibobo.lease.common.util.ListCopyToVOUtil;
import cn.ibobo.lease.common.util.PaginationVOUtil;
import cn.ibobo.lease.common.util.ResultVOUtil;
import cn.ibobo.lease.common.vo.ResultVO;
import cn.ibobo.lease.common.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 会员信息表控制器
 * @Author lililiang
 * @Date 2018-12-13 18:36:40
 */

@RestController
@Api(tags = "会员管理")
@RequestMapping("user")
public class UserController {

    private static final Log LOGGER = LogFactory.getLog(UserController.class);

    @Autowired
    private UserServiceImpl service;
    @Autowired
    private SmsCodeServiceImpl smsCodeService;
    @Autowired
    private ResultVOUtil<Object> resultObject;

    /**
     * 会员信息表详细信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "会员信息表详细信息", response = UserVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") String id){
        try {
            UserDTO entityDTO = service.queryOneByPk(id);
            UserVO entity = new UserVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存会员信息表信息
     * @Author lililiang
     * @Date 2018-12-13 18:36:40
     * @Param
     * @Return
     */
    @ApiOperation(value = "注册/修改会员信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(
            @ApiParam(value = "会员id，注册时不输入，修改时输入",required = false) @RequestParam(required = false) String uid,
            @ApiParam(value = "昵称",required = false) @RequestParam(required = false) String nickname,
            @ApiParam(value = "头像",required = false) @RequestParam(required = false) String headurl,
            @ApiParam(value = "手机号，注册时必须输入",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "验证码，注册时必须输入",required = false) @RequestParam(required = false) String code,
            @ApiParam(value = "账号",required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "配送地址",required = false) @RequestParam(required = false) String address,
            @ApiParam(value = "登陆密码",required = false) @RequestParam(required = false) String passwd
    ) {
        try {
            UserDTO entityDTO = new UserDTO();
            entityDTO.setUid(uid);
            entityDTO.setNickname(nickname);
            entityDTO.setHeadurl(headurl);
            entityDTO.setMobile(mobile);
            entityDTO.setName(name);
            entityDTO.setPasswd(passwd);
            entityDTO.setCode(code);
            entityDTO.setAddress(address);
            Integer save = service.save(entityDTO);
            return resultObject.success(save, ResultEnum.getMsg(save));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * @Description 密码找回
     * @Author lililiang
     * @Date 2019/3/4 14:26
     * @Param [uid, nickname, headurl, mobile, code, name, address, passwd]
     * @Return cn.ibobo.lease.common.vo.ResultVO<java.lang.Object>
     */
    @ApiOperation(value = "密码找回", response = ResultVO.class)
    @PostMapping("changePassWord")
    public ResultVO<Object> save(
            @ApiParam(value = "手机号",required = false) @RequestParam(required = false) String mobile,
            @ApiParam(value = "验证码",required = true) @RequestParam(required = true) String code,
            @ApiParam(value = "新密码",required = true) @RequestParam(required = true) String passwd
    ) {
        try {
            UserDTO entity = new UserDTO();
            entity.setMobile(mobile);
            entity.setPasswd(passwd);
            entity.setCode(code);
            Integer save = service.changePassWord(entity);
            return resultObject.success(save, ResultEnum.getMsg(save));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    @PostMapping("sendSms")
    @ApiOperation("获取短信验证码")
    public ResultVO<Object> sendSms(
            @ApiParam(value = "手机号", required = true) @RequestParam(required = true) String mobile,
            @ApiParam(value = "模板编码，SMS_162522056修改密码  SMS_156277781注册密码", required = true)
            @RequestParam(required = true, defaultValue = "SMS_156277781") String typeCode
    ){
        Integer code = smsCodeService.sendSms(mobile, typeCode);
        if(code == 1){
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
        }else{
            return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());

        }
    }



}